package org.spiniper.im.serivce.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.ListCellRenderer;
import javax.swing.plaf.metal.MetalButtonUI;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import org.spiniper.im.serivce.utils.ImagesUtils;

public class MainFrame extends JFrame {
	private static final long serialVersionUID = 7227564052851052838L;

	public MainFrame() throws HeadlessException {
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("欢迎使用spiniper Im 服务器运用系统");
		this.setResizable(false);
		int y=(int)(this.getToolkit().getScreenSize().getHeight()/2-300);
		int x=(int)(this.getToolkit().getScreenSize().getWidth()/2-400);
		this.setBounds(x, y, 800, 600);
		this.setLayout(new BorderLayout());
		this.add(mainPanel,BorderLayout.CENTER);
	}
	
	
	public final JTable clientInfo=new JTable(new AbstractTableModel(){
		private static final long serialVersionUID = 1L;
		private List<List<String>> rows=new ArrayList<>();	
		private ArrayList<String> cname=new ArrayList<>();
			{
				cname.add("Id");
				cname.add("名称");
				cname.add("地址");
				cname.add("状态");
				
				
			}
			
			public String getColumnName(int column) {
				return cname.get(column);
			};
			
			public int getRowCount() {
				return rows.size();
			}
	
			
			public int getColumnCount() {
				return cname.size();
			}
	
			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return false;
			}
			
			public Object getValueAt(int rowIndex, int columnIndex) {
				if(rows.get(rowIndex).size()<1){
					return null;
				}
				return rows.get(rowIndex).get(columnIndex);
			}
			
			public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
				if(rows.get(rowIndex)==null){
					rows.set(rowIndex, new ArrayList<String>());
				}
				rows.get(rowIndex).add(String.valueOf(aValue));
                fireTableCellUpdated(rowIndex, columnIndex);
			};
		
	}){
		
		private static final long serialVersionUID = 1L;

		{
			this.setFillsViewportHeight(true);
			this.setVisible(true);
			TableModel tm=this.getModel();
			AbstractTableModel dtm=(AbstractTableModel)tm;
			JButton b1=new JButton("1");
			JButton b2=new JButton("2");
			JButton b3=new JButton("3");
			JButton b4=new JButton("4");
//			dtm.addRow(new JButton[]{b1,b2,b3,b4});
		}
	};
	
	@SuppressWarnings({ "unchecked", "serial" })
	public final JPanel mainPanel=new JPanel(){
		private static final long serialVersionUID = 2473075883901335993L;
		{
			this.setLayout(new BorderLayout());
			
			this.add(new JTabbedPane(){
				{
					this.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
					this.addTab("控制面板", new JPanel(){
						{
							this.setLayout(new BorderLayout());
							this.add(new JPanel(){
								{
									this.setLayout(new FlowLayout(FlowLayout.LEFT));
									this.add(new JLabel("搜索："){
										{
											this.setFont(new Font("宋体",Font.BOLD,16));
										}
									});
									this.add(new JTextField(){
										{
											this.setColumns(40);
											this.setPreferredSize(new Dimension(200, 28));
											this.setFont(new Font("宋体", Font.PLAIN, 16));
										}
									});
									this.add(new JButton(new ImageIcon(ImagesUtils.getIcons("search.png"))){
										{
											
										}
									});
								}
							},BorderLayout.NORTH);
							
							this.add(new JScrollPane(clientInfo),BorderLayout.CENTER);
							this.add(new JToolBar("控制工具"){
								private static final long serialVersionUID = 1L;
								{
									this.addSeparator();
									this.add(new JButton(new ImageIcon(ImagesUtils.getIcons("start.png"))){
										{
											this.setEnabled(true);
											this.setToolTipText("启动服务");
										}
									});
									
									this.add(new JButton(new ImageIcon(ImagesUtils.getIcons("stop.png"))){
										{
											this.setEnabled(true);
											this.setToolTipText("停止服务");
										}
									});
									
									this.add(new JButton(new ImageIcon(ImagesUtils.getIcons("disconn.png"))){
										{
											this.setEnabled(false);
											this.setToolTipText("断开连接");
										}
									});
									this.addSeparator();
									this.addSeparator();
									this.add(new JButton(new ImageIcon(ImagesUtils.getIcons("top.png"))){
										{
											this.setEnabled(true);
											this.setToolTipText("首页");
										}
									});
									this.add(new JButton(new ImageIcon(ImagesUtils.getIcons("pre.png"))){
										{
											this.setEnabled(true);
											this.setToolTipText("上一页");
										}
									});
									this.add(new JButton(new ImageIcon(ImagesUtils.getIcons("next.png"))){
										{
											this.setEnabled(true);
											this.setToolTipText("下一页");
										}
									});
									this.add(new JButton(new ImageIcon(ImagesUtils.getIcons("last.png"))){
										{
											this.setEnabled(true);
											this.setToolTipText("末页");
										}
									});
								}
							},BorderLayout.SOUTH);
							this.setVisible(true);
						}
					});
					this.addTab("日志信息", new JPanel(){
						private static final long serialVersionUID = 1L;
						{
							this.setLayout(new BorderLayout());
							this.add(new JToolBar("日志工具"){
								{
									this.add(new JLabel("级别："));
									this.add(new JComboBox(){
										{
											this.addItem("debug");
											this.addItem("info");
											this.addItem("warn");
											this.addItem("error");
											this.setRenderer(new ListCellRenderer<Object>() {
												public Component getListCellRendererComponent(
														JList list,
														Object value, int index,
														boolean isSelected,
														boolean cellHasFocus) {
													
													return new JLabel(value.toString());
												}
											});
										}
									});
									
									this.addSeparator();
									this.addSeparator();
									this.add(new JLabel("缓存："));
									this.add(new JComboBox(){
										{
											this.addItem("1000");
											this.addItem("2000");
											this.addItem("4000");
											this.addItem("8000");
											this.setRenderer(new ListCellRenderer<Object>() {
												public Component getListCellRendererComponent(
														JList list,
														Object value, int index,
														boolean isSelected,
														boolean cellHasFocus) {
													return new JLabel(value.toString());
												}
											});
										}
									});
									
									this.addSeparator();
									this.addSeparator();
									this.add(new JButton(new ImageIcon(ImagesUtils.getIcons("clear.png"))){
										{
											this.setToolTipText("清理日志");
										}
									});
									this.add(new JButton(new ImageIcon(ImagesUtils.getIcons("export.png"))){
										{
											this.setToolTipText("导出日志");
										}
									});
								}
							},BorderLayout.NORTH);
							this.setVisible(true);
						}
					});
					this.setVisible(true);
				}
			},BorderLayout.CENTER);
			
			
			
			this.setVisible(true);
			
		}
	};
	
}
